package com.yao.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.system.entity.Company;
import com.yao.system.entity.PageResult;
import com.yao.system.entity.Result;
import com.yao.system.entity.User;
import com.yao.system.entity.dto.UserDTO;
import com.yao.system.mapper.CompanyMapper;
import com.yao.system.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yao.system.util.UserContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 霖
 * @since 2024-04-15
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Override
    public Result getCompanyInfo() {
        UserDTO user = UserContext.getUser();
        Company company = lambdaQuery()
                .eq(Company::getUserId, user.getId())
                .one();
        company.setCreateTime(null);
        return Result.ok(company);
    }

    @Override
    public Result updateCompanyInfo(Company company) {
        updateById(company);
        return Result.ok("更新成功");
    }

    @Override
    public Map<Long, Company> getByIds(Set<Long> companyIds) {
        List<Company> list = lambdaQuery()
                .in(Company::getId, companyIds)
                .list();
        Map<Long, Company> companyMap = list.stream()
                .collect(Collectors.toMap(Company::getId, company -> company));
        return companyMap;
    }



    @Override
    public Result queryCompanyList(Integer page, Integer size) {
        Page<Company> companyPage = lambdaQuery()
                .page(Page.of(page, size));
        List<Company> companyList = companyPage.getRecords();
        int total = (int) companyPage.getTotal();
        int pages = (int) companyPage.getPages();
        if (CollectionUtil.isEmpty(companyList)) {
            Result.ok(PageResult.of(total, pages, null));
        }
        return Result.ok(PageResult.of(total, pages, companyList));
    }

    @Override
    public Result updateCompanyAdmin(Company company) {
        updateById(company);

        return Result.ok("修改成功");
    }

}
